package com.hadluo.qq.server.controller;

import com.hadluo.qq.server.entity.Message;
import com.hadluo.qq.server.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 获取两个用户之间的历史消息
     * @param senderId 发送者ID
     * @param receiverId 接收者ID
     * @return 消息列表
     */
    @GetMapping("/history")
    public ResponseEntity<List<Message>> getHistoryMessages(
            @RequestParam("senderId") Long senderId,
            @RequestParam("receiverId") Long receiverId,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "20") Integer size) {
        List<Message> messages = messageService.getHistoryMessages(senderId, receiverId, page, size);
        return ResponseEntity.ok(messages);
    }

    /**
     * 获取用户的未读消息
     * @param userId 用户ID
     * @return 未读消息列表
     */
    @GetMapping("/unread/{userId}")
    public ResponseEntity<List<Message>> getUnreadMessages(@PathVariable("userId") Long userId) {
        List<Message> messages = messageService.getUnreadMessages(userId);
        return ResponseEntity.ok(messages);
    }

    /**
     * 标记消息为已读
     * @param messageId 消息ID
     */
    @PutMapping("/read/{messageId}")
    public ResponseEntity<Void> markMessageAsRead(@PathVariable("messageId") Long messageId) {
        messageService.markMessageAsRead(messageId);
        return ResponseEntity.ok().build();
    }
}